package com.wushi.lenovo.asia5b.app_ui.good.adapter;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mvp.images.WImageLoader;
import com.mvp.utils.ResString;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.good.bean.PriceDetailType;
import com.wushi.lenovo.asia5b.app_model.good.goodbean.OrderGoodBean;
import com.wushi.lenovo.asia5b.app_model.good.orderitem.OrderItemType;
import com.wushi.lenovo.asia5b.app_ui.h5.GoodsDetailsActivity;
import com.wushi.lenovo.asia5b.app_ui.orderdetail.PriceDetailActivity;
import com.wushi.lenovo.asia5b.app_widget.mingan.MinganImageView;

import java.util.List;

/**
 * Created by zhangyuncai on 2019/11/2.
 * 订单详情的商品样式
 */
public class OrderDetailGoodAdapter extends BaseQuickAdapter<OrderGoodBean, BaseViewHolder> {

    final OrderItemType orderItemType;

    public OrderDetailGoodAdapter(List<OrderGoodBean> data, OrderItemType orderItemType) {
        super(R.layout.item_goodadapter_orderdetaili, data);
        this.orderItemType = orderItemType;
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderGoodBean item) {

        TextView tv_orderdetail_type = helper.getView(R.id.tv_orderdetail_type);
//        if (orderItemType == OrderItemType.Payment) {
//            tv_orderdetail_type.setVisibility(View.GONE);
//        } else {
//            tv_orderdetail_type.setVisibility(View.VISIBLE);
//
//        }
        TextView tv_orderdetail_track = helper.getView(R.id.tv_orderdetail_track);
        if ((orderItemType == OrderItemType.Verification) || (orderItemType == OrderItemType.Problematic)) {
            tv_orderdetail_track.setVisibility(View.VISIBLE);

            //查看物流
            TextView tv_order_track = helper.getView(R.id.tv_orderdetail_track);
            if (item.show_parcel_track == 1) {
                tv_order_track.setVisibility(View.VISIBLE);
            } else {
                tv_order_track.setVisibility(View.GONE);
            }
            helper.addOnClickListener(R.id.tv_orderdetail_track);
        } else {
            tv_orderdetail_track.setVisibility(View.GONE);

        }

        //设置商品信息
        setGoodView(helper, item);

        //跳转商品详情
        startGoodsDetailsActivity(mContext, helper, item);
        //跳转订单价格详情
        startPriceDetailActivity(mContext, helper, item);

    }


    /**
     * 设置商品信息
     *
     * @param helper
     * @param item   商品数据
     */
    protected void setGoodView(BaseViewHolder helper, OrderGoodBean item) {


        //商品名称
        TextView tv_order_goodname = helper.getView(R.id.tv_orderdetail_goodname);
        tv_order_goodname.setText(item.getProduct_name());
        tv_order_goodname.setOnClickListener(v -> {
            GoodsDetailsActivity.startGoodsDetailsActivity(mContext, item.getProduct_id());
        });


        //包裹号
        TextView tv_order_goodno = helper.getView(R.id.tv_orderdetail_goodno);
        tv_order_goodno.setText(String.format("%s:%d", ResString.get(R.string.parcelno), item.getId()));

        //商品总价
        TextView tv_orderdetail_price = helper.getView(R.id.tv_orderdetail_price);
        tv_orderdetail_price.setText(String.format("%s        x %d", item.unit_price, item.getQuantity()));


        //商品订单状态
        TextView tv_order_type = helper.getView(R.id.tv_orderdetail_type);
        tv_order_type.setText(String.format("%s", item.getStatus_name()));
        //设置包裹状态背景颜色
        tv_order_type.setBackgroundColor(item.getStatus_color(mContext));

        //商品图片
        MinganImageView mingan_orderitem = helper.getView(R.id.mingan_orderdetail);
        WImageLoader.displayImage(mContext, item.getImage(), mingan_orderitem.getImageView());
        //是否显示敏感文字
        mingan_orderitem.setMinganTextVisibility(item.isMingan());


    }

    /**
     * 跳转订单详情界面
     *
     * @param context 上下文
     * @param helper
     * @param item    商品数据
     */
    protected void startGoodsDetailsActivity(Context context, BaseViewHolder helper, OrderGoodBean item) {
        MinganImageView mingan_orderdetail = helper.getView(R.id.mingan_orderdetail);
        if (mingan_orderdetail != null) {
            mingan_orderdetail.setOnClickListener(v -> {
                GoodsDetailsActivity.startGoodsDetailsActivity(context, item.getProduct_id());
            });
        }

    }

    /**
     * 跳转订单价格详情界面
     *
     * @param context 上下文
     * @param helper
     * @param item    商品数据
     */
    protected void startPriceDetailActivity(Context context, BaseViewHolder helper, OrderGoodBean item) {
        LinearLayout ll_orderdetail_price_vessel = helper.getView(R.id.ll_orderdetail_price_vessel);
        if (ll_orderdetail_price_vessel != null) {
            ll_orderdetail_price_vessel.setOnClickListener(v -> {
                PriceDetailActivity.startPriceDetailActivity(mContext, item, PriceDetailType.orderpack);
            });
        }

    }
}
